package org.spring.springboot.app.adv.advfamilylockinfo.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.adv.advarea.dao.AdvAreaDao;
import org.spring.springboot.app.adv.advarea.domain.po.AdvAreaPO;
import org.spring.springboot.app.adv.advarea.service.IAdvAreaService;
import org.spring.springboot.app.adv.advexam.domain.po.AdvExamPO;
import org.spring.springboot.app.adv.advexam.service.IAdvExamService;
import org.spring.springboot.app.adv.advfamilylockinfo.dao.AdvFamilyLockInfoDao;
import org.spring.springboot.app.adv.advfamilylockinfo.dao.AdvFamilylockLogDao;
import org.spring.springboot.app.adv.advfamilylockinfo.domain.po.AdvFamilyLockInfoPO;
import org.spring.springboot.app.adv.advfamilylockinfo.domain.vo.*;
import org.spring.springboot.app.adv.advfamilylockinfo.service.IAdvFamilyLockInfoService;
import org.spring.springboot.app.adv.advinfo.dao.AdvInfoDao;
import org.spring.springboot.app.adv.advinfo.domain.po.AdvInfoPO;
import org.spring.springboot.app.agent.balancelog.domain.po.SysOfficeBalanceLogPO;
import org.spring.springboot.app.agent.balancelog.service.ISysOfficeBalanceLogService;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.office.domain.po.SysOfficeWalletPO;
import org.spring.springboot.app.sys.office.service.ISysOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AdvFamilyLockInfoServiceImpl implements IAdvFamilyLockInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdvFamilyLockInfoServiceImpl.class);

    @Autowired
    private AdvFamilyLockInfoDao advFamilyLockInfoDao;
    @Autowired
    private AdvFamilylockLogDao advFamilylockLogDao;
    @Autowired
    private AdvInfoDao advInfoDao;
    @Autowired
    private ISysOfficeService sysOfficeService;
    @Autowired
    private ISysOfficeBalanceLogService sysOfficeBalanceLogService;
    @Autowired
    private IAdvExamService advExamService;
    @Autowired
    private IAdvAreaService advAreaService;


    @Override
    public AdvFamilyLockInfoPO find(String id) {
        return advFamilyLockInfoDao.selectByPrimaryKey(id);
    }

    @Override
    public R delete(String id) {
        R r = new R();
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(id);
        mainPo.preDelete();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) == 0) {
            return r.fail().setMessage("保存失败");
        }
        AdvFamilyLockInfoPO po = new AdvFamilyLockInfoPO();
        po.setId(id);
        po.preDelete();
        if (advFamilyLockInfoDao.updateByPrimaryKeySelective(po) == 0) {
            return r.fail().setMessage("保存失败");
        }
        return r.success();
    }


    @Override
    public List<AdvFamilyLockInfoPO> findPage(AdvFamilyLockInfoSearchVO searchVO, int pageNum, int pageSize) {
        return advFamilyLockInfoDao.findPage(searchVO, pageNum,pageSize);
    }
    @Override
    public List<AdvFamilylockLogVO> findPageLog(AdvFamilylockLogSearchVO searchVO, int pageNum, int pageSize) {
        return advFamilylockLogDao.findPage(searchVO, pageNum,pageSize);
    }

    @Override
    @Transactional
    public R saveInfo(AdvFamilyLockInfoSaveInVO vo, User user) {
        R r = new R();
        double eachCommision = 0.83;
        int totalCount = vo.getTotalCount();
        BigDecimal eachCommisionMo= new BigDecimal(eachCommision);
        BigDecimal allCommisionMo= eachCommisionMo.multiply(new BigDecimal(totalCount));

        BigDecimal payMo = allCommisionMo;
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_4);
        mainPO.setIsSystem(0);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_1);
        mainPO.setPayMo(payMo);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvFamilyLockInfoPO FamilyLockInfoPO = new AdvFamilyLockInfoPO();
        FamilyLockInfoPO.setLogoPhotos(vo.getLogoPhotos());
        FamilyLockInfoPO.setEachCommisionMo(eachCommisionMo);
        FamilyLockInfoPO.setAllCommisionMo(allCommisionMo);
        FamilyLockInfoPO.setTotalCount(vo.getTotalCount());
        FamilyLockInfoPO.setViewCount(0);
        FamilyLockInfoPO.preInsert();
        FamilyLockInfoPO.setId(mainPO.getId());
        if (advFamilyLockInfoDao.insertSelective(FamilyLockInfoPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        //保存区域
        advAreaService.saveAreas(mainPO.getId(),vo.getAreaIds());

        AdvFamilyLockInfoSaveOutVO outVO = new AdvFamilyLockInfoSaveOutVO();
        outVO.setId(mainPO.getId());
        outVO.setPayMo(mainPO.getPayMo());
        return r.success().setData(outVO);
    }




    @Override
    public R updateInfo(AdvFamilyLockInfoUpdateInVO vo) {
        R r = new R();
        //判断广告是否审核（审核之前可以修改）
        AdvFamilyLockInfoPO FamilyLockInfoPO = advFamilyLockInfoDao.selectByPrimaryKey(vo.getId());
        if (FamilyLockInfoPO.getExamState() != null && FamilyLockInfoPO.getExamState().intValue() == Constants.ADV_EXAM_STATE_2) {
            return r.fail().setMessage("已审核广告不能修改");
        }

        //保存主表
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(vo.getId());
        mainPo.setTitle(vo.getTitle());
        mainPo.setContent(vo.getContent());
        mainPo.setPhotos(vo.getPhotos());
        mainPo.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) <1) {
            return r.fail().setMessage("修改活动主表失败");
        }
        if (StringUtils.isNotBlank(vo.getLogoPhotos())) {
            AdvFamilyLockInfoPO FamilyLockInfoPO1 = new AdvFamilyLockInfoPO();
            FamilyLockInfoPO1.setId(vo.getId());
            FamilyLockInfoPO1.setLogoPhotos(vo.getLogoPhotos());
            FamilyLockInfoPO1.preUpdate();
            if (advFamilyLockInfoDao.updateByPrimaryKeySelective(FamilyLockInfoPO1) <1) {
                return r.fail().setMessage("修改活动副表失败");
            }
        }

        if (StringUtils.isNotBlank(vo.getAreaIds())) {
            //删除区域
            advAreaService.deleteByAdvId(mainPo.getId());
            //保存区域
            advAreaService.saveAreas(mainPo.getId(),vo.getAreaIds());
        }
        return r.success();
    }



    @Override
    @Transactional
    public R pay(String id, User user) {
        R r = new R();
        AdvInfoPO mainPO = advInfoDao.selectByPrimaryKey(id);
        //判断支付状态
        if (mainPO.getPayState().intValue() == Constants.ADV_INFO_TYPE_PAY_STATE_2) {
            return r.fail().setMessage("活动已经支付");
        }
        //比对机构金额和广告费用
        SysOfficeWalletPO walletPO = sysOfficeService.queryWallet(user.getOfficeId());
        BigDecimal balance = walletPO.getBalance();
        BigDecimal payMo = mainPO.getPayMo();
        if (payMo.compareTo(balance) > 0) {
            return  r.fail().setMessage("余额不足");
        }
        //扣除机构费用
        sysOfficeService.subOfficeMo(user.getOfficeId(),payMo,Constants.SYS_OFFICE_BALANCE_LOG_TYPE_103,id,null,mainPO.getTitle());
        //添加审核表
        advExamService.addExam(user.getOfficeId(),id);
        //修改广告状态
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_2);
        mainPO.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPO) < 1) {
            throw new RuntimeException("更新活动主表失败");
        }
        return r.success();
    }

    @Override
    public int profitDivision(String advId) {
        //家庭锁广告暂时没有分润，每次开门进行划分
        return 1;
    }

    @Override
    public R saveSystemInfo(AdvFamilyLockInfoSystemSaveInVO vo, User user) {
        R r = new R();
        double eachCommision = 0.5;
        int totalCount = vo.getTotalCount();
        BigDecimal eachCommisionMo= new BigDecimal(eachCommision);
        BigDecimal allCommisionMo= eachCommisionMo.multiply(new BigDecimal(totalCount));
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_4);
        mainPO.setIsSystem(1);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_0);
        mainPO.setPayMo(null);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvFamilyLockInfoPO FamilyLockInfoPO = new AdvFamilyLockInfoPO();
        FamilyLockInfoPO.setLogoPhotos(vo.getLogoPhotos());
        FamilyLockInfoPO.setEachCommisionMo(eachCommisionMo);
        FamilyLockInfoPO.setAllCommisionMo(allCommisionMo);
        FamilyLockInfoPO.setTotalCount(vo.getTotalCount());
        FamilyLockInfoPO.setViewCount(0);
        FamilyLockInfoPO.preInsert();
        FamilyLockInfoPO.setId(mainPO.getId());
        if (advFamilyLockInfoDao.insertSelective(FamilyLockInfoPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        return r.success();
    }


}
